﻿Imports Microsoft.Office.Interop.Excel


Public Class CUtileria
  
  Public Shared function  GrabarBitacora(ByVal modulo As String, ByVal funcion As String, ByVal descripcion As String) As Integer 

    
        Dim regNuevo As New CRegistroBitacora ()
        Dim id As Integer 


        regNuevo.FechaIni = CServiciosMacrosSyBaseDB.FechaHoyDB()
        regNuevo.usuario = CServiciosMacrosSyBaseDB.USUARIO
        regNuevo.Modulo = modulo 
        regNuevo.funcion = funcion

        If descripcion.Length > 1000 THEN 
           regNuevo.descripcion =  descripcion.Substring(0,1000)
        else
           regNuevo.descripcion =  descripcion
        End If        
         
        Try
         
         id = CServicioLocalSQLServerDB.GrabarBitacoraBD(regNuevo) 

         if id <= -1 then 
            Return -1
         else
            Return id
         End If
        
        
        Catch ex As Exception
            Return -1
        End Try
        
   End function
 
  Public Shared Sub CompletarRegBitacora(ByVal id As Integer )
       Try
            CServicioLocalSQLServerDB.CompletarFechaFin(id, CServiciosMacrosSyBaseDB.FechaHoyDB())
       Catch ex As Exception
       End Try      
    End Sub

  Public Shared Function  ObtenerDatosClipboard(ByVal separadorRenglon As Char, ByVal separadorColumna As Char) As CDatosEnFormatoX
        Dim datos          As new CDatosEnFormatoX()
        Dim strTexto       As String 
        Dim auxRenglones() As String 
        Dim numRen         As Integer 


         Try

          
          
            

            strTexto = Clipboard.GetText()
            auxRenglones = strTexto.Split(separadorRenglon)

            If auxRenglones(auxRenglones.Count - 1).Split (separadorColumna).Count = 1 and auxRenglones(auxRenglones.Count - 1).Split (separadorColumna)(0).Equals("")  then
               numRen = auxRenglones.Count -1
            else
               numRen = auxRenglones.Count
            End If

        
            datos.NumeroRenglones = numRen

            Dim matrix(numRen-1)()  As String            
            Dim i As Integer = 0
            Dim limRen As Integer 

            ReDim datos.NumeroColumnas(numRen-1)


            limRen = numRen-1  

            For i=0 to limRen                                                          
                matrix(i) = auxRenglones(i).Split (separadorColumna)
                datos.NumeroColumnas(i) = matrix(i).Count 
            Next
                        

            datos.NumeroRenglones = matrix.Count  
            datos.Datos = matrix 
                 
                        
         Catch ex As Exception
             MessageBox.Show (ex.Message)
             Return Nothing 
         End Try  


         Return datos 
       
    End Function

  Public Shared function ExportarAExcel(ByRef miDataGrid As DataGrid, ByVal regInicio As Integer, ByVal colInicio As Integer ) As _Application 

    Dim xlsAplicacion As _Application
    Dim wbkLibro      As Workbook
    Dim shtHoja       As Worksheet

    Dim iReg As Integer 
    Dim iCol As Integer 



    Dim lstInterAux As List(Of CFondoIntermediario ) = miDataGrid.ItemsSource 


    Try
        xlsAplicacion = CreateObject("Excel.Application")        
    Catch ex As Exception
        xlsAplicacion = CreateObject("Excel.Application")
    End Try
    Try
        xlsAplicacion.SheetsInNewWorkbook = 1
        xlsAplicacion.DisplayAlerts = False
        xlsAplicacion.Visible = True
    Catch ex As Exception
        MessageBox.Show ("Error al intentar abril Excel","Exportación a Excel",MessageBoxButton.OK,MessageBoxImage.Exclamation  ) 
        Return Nothing 
    End Try

 
    wbkLibro = xlsAplicacion.Workbooks.Add
    shtHoja = wbkLibro.Sheets(1)
    

    'Encabezado    
     For iCol  = 0 to miDataGrid.Columns.Count - 1 
          shtHoja.Cells(regInicio,(iCol + 1) + colInicio ).value =  miDataGrid.Columns(iCol).Header            
     Next
        
    'Datos
    For iReg   = 0 to miDataGrid.Items.Count - 1 
         For iCol  = 0 to miDataGrid.Columns.Count - 1 

            If (IsDate(lstInterAux(iReg)(iCol).ToString ())) and (lstInterAux(iReg)(iCol).ToString ().IndexOf("/") >= 0 or lstInterAux(iReg)(iCol).ToString ().IndexOf("-") >= 0 ) THEN
               shtHoja.Cells(iReg + regInicio + 1, (iCol + 1) + colInicio).NumberFormat = "@"
            End If

            shtHoja.Cells(iReg + regInicio + 1, (iCol + 1) + colInicio).value = lstInterAux(iReg)(iCol).ToString () 
         Next                                        
    Next


    Return xlsAplicacion 

End Function 

  Public Enum Condiciones
     sinCondicion   = 0
     pegarMonMaxLiq = 1     
  End Enum


  Public Shared Function  PrepararCadenaParaSQL(ByVal Valor As String, ByVal longitud As Integer) As String
      If Valor Is Nothing Then
            Return ""
      End If
      If Valor.Length > longitud then 
         Return Valor.Substring(0,longitud -1 ).Replace ("'","%").Trim   
      else
         Return Valor.Replace ("'","%").Trim   
      End If
  End Function
  





End Class
